package com.wst.wst_server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.wst_server.entity.Org;
import com.wst.wst_server.entity.Orgplus;
import com.wst.wst_server.service.IOrgService;
import com.wst.wst_server.service.IOrgplusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ��֯������ 前端控制器
 * </p>
 *
 * @author zyk
 * @since 2023-03-24
 */
@RestController
@RequestMapping("/org")
public class OrgController {
    @Autowired
    IOrgService service;


    @RequestMapping("/add")
    public ResponseResult add(Org org, Orgplus orgplus){
        System.out.println(org);
        System.out.println(orgplus);
        boolean b = service.add(org,orgplus);
        return new ResponseResult(b?200:500,b?"添加成功":"添加失败",null);
    }
    @RequestMapping("/page")
    public ResponseResult<IPage<Org>> page(Integer page, Integer count,@RequestParam Map<String,Object> map){
        System.out.println(map);
        IPage<Org> obj = null;
        if(map.size()<=2){
            obj = service.page(new Page<>(page,count),new QueryWrapper<Org>().orderByDesc("id"));
        }else {
            obj = service.getbycondition(map);
        }
        return new ResponseResult<IPage<Org>>(200,"ok",obj);
    }
    @RequestMapping("/getbyid")
    public ResponseResult<Map<String,Object>> getbyid(Integer id){
        Map<String,Object> map = service.getId(id);
        return new ResponseResult<Map<String,Object>>(200,"ok",map);
    }
    @RequestMapping("/getbycondition")
    public ResponseResult<IPage<Org>> getcondition(@RequestParam Map<String,Object> map){
        System.out.println(map);
        IPage<Org> ipage = service.getbycondition(map);
        return new ResponseResult<IPage<Org>>(200,"ok",ipage);
    }

    @RequestMapping("/update")
    public ResponseResult update(Org org, Orgplus orgplus){
        service.myupdate(org,orgplus);
        return new ResponseResult(200,"ok",null);
    }

    @RequestMapping("/getallorg")
    public ResponseResult<List<Org>> getcondition(){
        return new ResponseResult<List<Org>>(200,"ok",  service.list(new QueryWrapper<Org>().orderByDesc("id")));
    }

    @RequestMapping("/getorgandorgplus")
    public ResponseResult<IPage<Map<String, Object>>> getorgandorgplus(@RequestParam Map<String, Object> map){
        return new ResponseResult<IPage<Map<String, Object>>>(200,"ok", service.getorgandorgplus(map));
    }

    @RequestMapping("/getorgnameandyearly")
    public ResponseResult<Map<String, Object>> getorgnameandyearly(@RequestParam Map<String, Object> map){
        System.out.println(map);
        return new ResponseResult<Map<String, Object>>(200,"ok", service.getorgnameandyearly(map));
    }

}
